using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.UI;
using PagedList;
using PagedList.Mvc;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using <Application>.Models;

namespace <Application>.Controllers
{
    public class <OwnerU>_<TableU>Controller : Controller
    {
        private <Database>DBContext db = new <Database>DBContext();
        
        // GET: /<OwnerU>_<TableU>/
        public ActionResult Index(string sortOrder,  
                                  String SearchField,
                                  String SearchCondition,
                                  String SearchText,
                                  String Export,
                                  int? PageSize,
                                  int? page, 
                                  string command)
        {

            if (command == "Show All") {
                SearchField = null;
                SearchCondition = null;
                SearchText = null;
                Session["SearchField"] = null;
                Session["SearchCondition"] = null;
                Session["SearchText"] = null; } 
            else if (command == "Add New Record") { return RedirectToAction("Create"); } 
            else if (command == "Export") { Session["Export"] = Export; } 
            else if (command == "Search" | command == "Page Size") {
                if (!string.IsNullOrEmpty(SearchText)) {
                    Session["SearchField"] = SearchField;
                    Session["SearchCondition"] = SearchCondition;
                    Session["SearchText"] = SearchText; }
                } 
            if (command == "Page Size") { Session["PageSize"] = PageSize; }

    <If Loop>
	        <IsNotText>
	        <And>
	        <IsNotNText>
            ViewData["SearchFields"] = GetFields((Session["SearchField"] == null ? "<FieldC>" : Convert.ToString(Session["SearchField"])));
        <ExitLoop>
    </If Loop>
            ViewData["SearchConditions"] = Library.GetConditions((Session["SearchCondition"] == null ? "Contains" : Convert.ToString(Session["SearchCondition"])));
            ViewData["SearchText"] = Session["SearchText"];
            ViewData["Exports"] = Library.GetExports((Session["Export"] == null ? "Pdf" : Convert.ToString(Session["Export"])));
            ViewData["PageSizes"] = Library.GetPageSizes();

            ViewData["CurrentSort"] = sortOrder;
    <If Loop>
	        <IsNotText>
	        <And>
	        <IsNotNText>
            ViewData["<FieldU>SortParm"] = sortOrder == "<FieldU>_asc" ? "<FieldU>_desc" : "<FieldU>_asc";
    </If Loop>

            var Query = db.<OwnerU>_<TableU>.AsQueryable();

            try {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["SearchField"])) && !string.IsNullOrEmpty(Convert.ToString(Session["SearchCondition"])) && !string.IsNullOrEmpty(Convert.ToString(Session["SearchText"])))
                {
                    SearchField = Convert.ToString(Session["SearchField"]);
                    SearchCondition = Convert.ToString(Session["SearchCondition"]);
                    SearchText = Convert.ToString(Session["SearchText"]);

                    if (SearchCondition == "Contains") {
                        Query = Query.Where(p => 
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.Value.ToString().Contains(SearchText)) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.Value.ToString().Contains(SearchText)) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().Contains(SearchText.Trim().ToLower())) 
    </If Loop>
                                         );
                    } else if (SearchCondition == "Starts with...") {
                        Query = Query.Where(p => 
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.Value.ToString().StartsWith(SearchText)) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                                                 ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<Field>.Value.ToString().StartsWith(SearchText)) 
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                                                 || ("<FieldC>".ToString().Equals(SearchField) && p.<ROwner>_<RTable>.<RDisplayField>.ToString().Trim().ToLower().StartsWith(SearchText.Trim().ToLower())) 
    </If Loop>
                                         );
                    } else if (SearchCondition == "Equals") {
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        if ("<FieldC>".Equals(SearchField)) { var m<Field> = <ConvertRDisplayField>(SearchText); Query = Query.Where(p => p.<ROwner>_<RTable>.<RDisplayField> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow=[0]>
                        if ("<FieldC>".Equals(SearchField)) { var m<Field> = <ConvertRDisplayField>(SearchText); Query = Query.Where(p => p.<ROwner>_<RTable>.<RDisplayField> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow=[0]>
                        if ("<FieldC>".Equals(SearchField)) { var m<Field> = <ConvertRDisplayField>(SearchText); Query = Query.Where(p => p.<ROwner>_<RTable>.<RDisplayField> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotNull>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <ConvertRDisplayField>(SearchText); Query = Query.Where(p => p.<ROwner>_<RTable>.<RDisplayField> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsNotRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNotNullable>
   	<And>
        <IsRDisplayField>
   	<And>
   	<IsCurRow<>[0]>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <ConvertRDisplayField>(SearchText); Query = Query.Where(p => p.<ROwner>_<RTable>.<RDisplayField> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsNotRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> == m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
   	<IsNull>
   	<And>
   	<IsNullable>
   	<And>
        <IsRDisplayField>
        <And>
   	<IsCurRow<>[0]>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <ConvertRDisplayField>(SearchText); Query = Query.Where(p => p.<ROwner>_<RTable>.<RDisplayField> == m<Field>); }
    </If Loop>
                    } else if (SearchCondition == "More than...") { 
                        if (SearchField.Equals(SearchCondition)) { }
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotRelated>
   	<And>
        <IsNumber>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> > m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsRelated>
   	<And>
        <IsNotRDisplayField>
   	<And>
        <IsNumber>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> > m<Field>); }
        <IsDateTimePicker>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> > m<Field>); }
</If Loop>
                    } else if (SearchCondition == "Less than...") { 
                        if (SearchField.Equals(SearchCondition)) { }
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotRelated>
   	<And>
        <IsNumber>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> < m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsRelated>
   	<And>
        <IsNotRDisplayField>
   	<And>
        <IsNumber>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> < m<Field>); }
        <IsDateTimePicker>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> < m<Field>); }
</If Loop>
                    } else if (SearchCondition == "Equal or more than...") { 
                        if (SearchField.Equals(SearchCondition)) { }
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotRelated>
   	<And>
        <IsNumber>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> >= m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsRelated>
   	<And>
        <IsNotRDisplayField>
   	<And>
        <IsNumber>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> >= m<Field>); }
        <IsDateTimePicker>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> >= m<Field>); }
</If Loop>
                    } else if (SearchCondition == "Equal or less than...") { 
                        if (SearchField.Equals(SearchCondition)) { }
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsNotRelated>
   	<And>
        <IsNumber>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> <= m<Field>); }
   	<IsNotText>
   	<And>
   	<IsNotNText>
   	<And>
        <IsRelated>
   	<And>
        <IsNotRDisplayField>
   	<And>
        <IsNumber>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> <= m<Field>); }
        <IsDateTimePicker>
                        else if ("<FieldC>".Equals(SearchField)) { var m<Field> = <Convert>(SearchText); Query = Query.Where(p => p.<Field> <= m<Field>); }
</If Loop>
                    }
                }
            } catch (Exception) { }

            switch (sortOrder)
            {
    <If Loop>
	        <IsNotRelated>
	        <And>
	        <IsNotText>
	        <And>
	        <IsNotNText>
                case "<FieldU>_desc":
                    Query = Query.OrderByDescending(s => s.<FieldU>);
                    break;
                case "<FieldU>_asc":
                    Query = Query.OrderBy(s => s.<FieldU>);
                    break;
	        <IsRelated>
	        <And>
	        <IsNotText>
	        <And>
	        <IsNotNText>
                case "<FieldU>_desc":
                    Query = Query.OrderByDescending(s => s.<ROwner>_<RTable>.<RDisplayField>);
                    break;
                case "<FieldU>_asc":
                    Query = Query.OrderBy(s => s.<ROwner>_<RTable>.<RDisplayField>);
                    break;
    </If Loop>
                default:  // Name ascending 
                    Query = Query.OrderBy(s => s.<FieldU>);
                    break;
            }

            if (command == "Export") {
                GridView gv = new GridView();
                DataTable dt = new DataTable();
<Loop>
                dt.Columns.Add("<FieldC>", typeof(string));
</Loop>
                foreach (var item in Query.ToList())
                {
                    dt.Rows.Add(
    <If Loop>
        <IsNotRelated>
        <And>
        <IsCurRow=[0]>
                        item.<FieldU>
        <IsNotRelated>
        <And>
        <IsCurRow<>[0]>
                       ,item.<FieldU>
        <IsRelated>
        <And>
        <IsCurRow=[0]>
                        item.<ROwner>_<RTable>.<RDisplayField>
        <IsRelated>
        <And>
        <IsCurRow<>[0]>
                       ,item.<ROwner>_<RTable>.<RDisplayField>
    </If Loop>
                    );
                }
                gv.DataSource = dt;
                gv.DataBind();
                ExportData(Export, gv, dt);
            }

            int pageNumber = (page ?? 1);
            int? pageSZ = (Convert.ToInt32(Session["PageSize"]) == 0 ? 5 : Convert.ToInt32(Session["PageSize"]));
            return View(Query.ToPagedList(pageNumber, (pageSZ ?? 5)));
        }

        // GET: /<OwnerU>_<TableU>/Details/<id>
        public ActionResult Details(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                      <VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                     ,<VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                      <VBType> <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                     ,<VBType> <FieldU>
    </If Loop>
                                   )
    <If Loop>
      <IsPKey>
        {
            if (
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                    <FieldU> == null
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                 || <FieldU> == null
    </If Loop>
               )
    <If Loop>
      <IsPKey>
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            <OwnerU>_<TableU> <OwnerU>_<TableU> = db.<OwnerU>_<TableU>.Find(
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 <FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                ,<FieldU>
    </If Loop>
                                            );
    <If Loop>
      <IsPKey>
            if (<OwnerU>_<TableU> == null)
            {
                return HttpNotFound();
            }
            return View(<OwnerU>_<TableU>);
        }
        <ExitLoop>
    </If Loop>

        // GET: /<OwnerU>_<TableU>/Create
        public ActionResult Create()
        {
    <If Loop>
      <IsFKey>
        // ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
            ViewData["<RFieldU>"] = new SelectList(db.<ROwnerU>_<RTableU>, "<RFieldU>", "<RDisplayFieldU>");
    </If Loop>

            return View();
        }

        // POST: /<OwnerU>_<TableU>/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include=
		            <If Loop>
                             <IsNotIdentity>
				   <SF[+ "," + ]>"<Field>"
		            </If Loop>
				  )] <OwnerU>_<TableU> <OwnerU>_<TableU>)
        {
            if (ModelState.IsValid)
            {
                db.<OwnerU>_<TableU>.Add(<OwnerU>_<TableU>);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    <If Loop>
      <IsFKey>
        // ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
            ViewData["<RFieldU>"] = new SelectList(db.<ROwnerU>_<RTableU>, "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>);
    </If Loop>

            return View(<OwnerU>_<TableU>);
        }

        // GET: /<OwnerU>_<TableU>/Edit/<id>
        public ActionResult Edit(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                   <VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                  ,<VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                   <VBType> <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                  ,<VBType> <FieldU>
    </If Loop>
                                )
    <If Loop>
      <IsPKey>
        {
            if (
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                    <FieldU> == null
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                 || <FieldU> == null
    </If Loop>
               )
    <If Loop>
      <IsPKey>
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            <OwnerU>_<TableU> <OwnerU>_<TableU> = db.<OwnerU>_<TableU>.Find(
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 <FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                ,<FieldU>
    </If Loop>
                                            );
    <If Loop>
      <IsPKey>
            if (<OwnerU>_<TableU> == null)
            {
                return HttpNotFound();
            }
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsFKey>
        // ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
            ViewData["<RFieldU>"] = new SelectList(db.<ROwnerU>_<RTableU>, "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>);
    </If Loop>

    <If Loop>
      <IsPKey>
            return View(<OwnerU>_<TableU>);
        }
        <ExitLoop>
    </If Loop>

        // POST: /<OwnerU>_<TableU>/Edit/<id>
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost, ActionName("Edit")]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(<OwnerU>_<TableU> <OwnerU>_<TableU>)
        {
            if (ModelState.IsValid)
            {
                db.Entry(<OwnerU>_<TableU>).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    <If Loop>
      <IsFKey>
        // ComboBox
      <ExitLoop>
    </If Loop>
    <If Loop>
    	<IsFKey>
            ViewData["<RFieldU>"] = new SelectList(db.<ROwnerU>_<RTableU>, "<RFieldU>", "<RDisplayFieldU>", <OwnerU>_<TableU>.<FieldU>);
    </If Loop>

            return View(<OwnerU>_<TableU>);
        }

        // GET: /<OwnerU>_<TableU>/Delete/<id>
        public ActionResult Delete(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                     <VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                    ,<VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                     <VBType> <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                    ,<VBType> <FieldU>
    </If Loop>
                                  )
    <If Loop>
      <IsPKey>
        {
            if (
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                    <FieldU> == null
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                 || <FieldU> == null
    </If Loop>
               )
    <If Loop>
      <IsPKey>
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            <OwnerU>_<TableU> <OwnerU>_<TableU> = db.<OwnerU>_<TableU>.Find(
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 <FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                ,<FieldU>
    </If Loop>
                                            );
    <If Loop>
      <IsPKey>
            if (<OwnerU>_<TableU> == null)
            {
                return HttpNotFound();
            }
            return View(<OwnerU>_<TableU>);
        }
        <ExitLoop>
    </If Loop>

        // POST: /<OwnerU>_<TableU>/Delete/<id>
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(
    <If Loop>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow=[0]>
                                            <VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNullable>
      <And>
      <IsCurRow<>[0]>
                                            ,<VBType>? <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow=[0]>
                                            <VBType> <FieldU>
      <IsPKey>
      <And>
      <IsNotNullable>
      <And>
      <IsCurRow<>[0]>
                                            ,<VBType> <FieldU>
    </If Loop>
                                            )
    <If Loop>
      <IsPKey>
        {
            <OwnerU>_<TableU> <OwnerU>_<TableU> = db.<OwnerU>_<TableU>.Find(
        <ExitLoop>
    </If Loop>
    <If Loop>
      <IsPKey>
      <And>
      <IsCurRow=[0]>
                                                 <FieldU>
      <IsPKey>
      <And>
      <IsCurRow<>[0]>
                                                ,<FieldU>
    </If Loop>
                                            );
    <If Loop>
      <IsPKey>
            db.<OwnerU>_<TableU>.Remove(<OwnerU>_<TableU>);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        <ExitLoop>
    </If Loop>

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private static List<SelectListItem> GetFields(String select)
        {
            List<SelectListItem> list = new List<SelectListItem>();
	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
            SelectListItem Item<iVarA> = new SelectListItem { Text = "<FieldC>", Value = "<FieldC>" };
        <iVarA=+[1]>
</If Loop>

	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
            <SF[else ]>if (select == "<FieldC>") { Item<iVarA>.Selected = true; }
        <iVarA=+[1]>
</If Loop>

	<iVarA=[1]>
<If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
            list.Add(Item<iVarA>);
        <iVarA=+[1]>
</If Loop>

            return list.ToList();
        }

        private void ExportData(String Export, GridView gv, DataTable dt)
        {
            if (Export == "Pdf")
            {
                PDFform pdfForm = new PDFform(dt, "<Head>", "Many");
                Document document = pdfForm.CreateDocument();
                PdfDocumentRenderer renderer = new PdfDocumentRenderer(true);
                renderer.Document = document;
                renderer.RenderDocument();

                MemoryStream stream = new MemoryStream();
                renderer.PdfDocument.Save(stream, false);

                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=" + "Report.pdf");
                Response.ContentType = "application/Pdf.pdf";
                Response.BinaryWrite(stream.ToArray());
                Response.Flush();
                Response.End();
            }
            else
            {
                Response.ClearContent();
                Response.Buffer = true;
                if (Export == "Excel")
                {
                    Response.AddHeader("content-disposition", "attachment;filename=" + "Report.xls");
                    Response.ContentType = "application/Excel.xls";
                }
                else if (Export == "Word")
                {
                    Response.AddHeader("content-disposition", "attachment;filename=" + "Report.doc");
                    Response.ContentType = "application/Word.doc";
                }
                Response.Charset = "";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);
                gv.RenderControl(htw);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }

    }
}
